<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="计划分类" prop="planAssort">
        <el-input
          v-model="queryParams.planAssort"
          placeholder="请输入计划分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划名称
" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入计划名称
"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划周期" prop="planCycle">
        <el-input
          v-model="queryParams.planCycle"
          placeholder="请输入计划周期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划进度" prop="planProgress">
        <el-input
          v-model="queryParams.planProgress"
          placeholder="请输入计划进度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划积分" prop="planPoints">
        <el-input
          v-model="queryParams.planPoints"
          placeholder="请输入计划积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否默认" prop="idDefault">
        <el-input
          v-model="queryParams.idDefault"
          placeholder="请输入是否默认"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="步数" prop="stepCount">
        <el-input
          v-model="queryParams.stepCount"
          placeholder="请输入步数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="睡眠质量" prop="sleepQuality">
        <el-input
          v-model="queryParams.sleepQuality"
          placeholder="请输入睡眠质量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="静息心率" prop="restingHeartRate">
        <el-input
          v-model="queryParams.restingHeartRate"
          placeholder="请输入静息心率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="心率变异性" prop="heartRateVariability">
        <el-input
          v-model="queryParams.heartRateVariability"
          placeholder="请输入心率变异性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="血氧饱和度" prop="bloodOxygenSaturation">
        <el-input
          v-model="queryParams.bloodOxygenSaturation"
          placeholder="请输入血氧饱和度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡路里" prop="calorie">
        <el-input
          v-model="queryParams.calorie"
          placeholder="请输入卡路里"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打卡事件" prop="checkInEvent">
        <el-input
          v-model="queryParams.checkInEvent"
          placeholder="请输入打卡事件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="情绪管理" prop="emotionManagement">
        <el-input
          v-model="queryParams.emotionManagement"
          placeholder="请输入情绪管理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ring:ring_health_plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ring:ring_health_plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ring:ring_health_plan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ring:ring_health_plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ring_health_planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="计划分类" align="center" prop="planAssort" />
      <el-table-column label="计划名称
" align="center" prop="planName" />
      <el-table-column label="计划周期" align="center" prop="planCycle" />
      <el-table-column label="计划进度" align="center" prop="planProgress" />
      <el-table-column label="计划积分" align="center" prop="planPoints" />
      <el-table-column label="是否默认" align="center" prop="idDefault" />
      <el-table-column label="计划状态" align="center" prop="planStatus" />
      <el-table-column label="步数" align="center" prop="stepCount" />
      <el-table-column label="睡眠质量" align="center" prop="sleepQuality" />
      <el-table-column label="静息心率" align="center" prop="restingHeartRate" />
      <el-table-column label="心率变异性" align="center" prop="heartRateVariability" />
      <el-table-column label="血氧饱和度" align="center" prop="bloodOxygenSaturation" />
      <el-table-column label="卡路里" align="center" prop="calorie" />
      <el-table-column label="打卡事件" align="center" prop="checkInEvent" />
      <el-table-column label="情绪管理" align="center" prop="emotionManagement" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ring:ring_health_plan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ring:ring_health_plan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改健康计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划分类" prop="planAssort">
          <el-input v-model="form.planAssort" placeholder="请输入计划分类" />
        </el-form-item>
        <el-form-item label="计划名称
" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称
" />
        </el-form-item>
        <el-form-item label="计划周期" prop="planCycle">
          <el-input v-model="form.planCycle" placeholder="请输入计划周期" />
        </el-form-item>
        <el-form-item label="计划进度" prop="planProgress">
          <el-input v-model="form.planProgress" placeholder="请输入计划进度" />
        </el-form-item>
        <el-form-item label="计划积分" prop="planPoints">
          <el-input v-model="form.planPoints" placeholder="请输入计划积分" />
        </el-form-item>
        <el-form-item label="是否默认" prop="idDefault">
          <el-input v-model="form.idDefault" placeholder="请输入是否默认" />
        </el-form-item>
        <el-form-item label="步数" prop="stepCount">
          <el-input v-model="form.stepCount" placeholder="请输入步数" />
        </el-form-item>
        <el-form-item label="睡眠质量" prop="sleepQuality">
          <el-input v-model="form.sleepQuality" placeholder="请输入睡眠质量" />
        </el-form-item>
        <el-form-item label="静息心率" prop="restingHeartRate">
          <el-input v-model="form.restingHeartRate" placeholder="请输入静息心率" />
        </el-form-item>
        <el-form-item label="心率变异性" prop="heartRateVariability">
          <el-input v-model="form.heartRateVariability" placeholder="请输入心率变异性" />
        </el-form-item>
        <el-form-item label="血氧饱和度" prop="bloodOxygenSaturation">
          <el-input v-model="form.bloodOxygenSaturation" placeholder="请输入血氧饱和度" />
        </el-form-item>
        <el-form-item label="卡路里" prop="calorie">
          <el-input v-model="form.calorie" placeholder="请输入卡路里" />
        </el-form-item>
        <el-form-item label="打卡事件" prop="checkInEvent">
          <el-input v-model="form.checkInEvent" placeholder="请输入打卡事件" />
        </el-form-item>
        <el-form-item label="情绪管理" prop="emotionManagement">
          <el-input v-model="form.emotionManagement" placeholder="请输入情绪管理" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRing_health_plan, getRing_health_plan, delRing_health_plan, addRing_health_plan, updateRing_health_plan } from "@/api/ring/ring_health_plan";

export default {
  name: "Ring_health_plan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 健康计划表格数据
      ring_health_planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planAssort: null,
        planName: null,
        planCycle: null,
        planProgress: null,
        planPoints: null,
        idDefault: null,
        planStatus: null,
        stepCount: null,
        sleepQuality: null,
        restingHeartRate: null,
        heartRateVariability: null,
        bloodOxygenSaturation: null,
        calorie: null,
        checkInEvent: null,
        emotionManagement: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询健康计划列表 */
    getList() {
      this.loading = true;
      listRing_health_plan(this.queryParams).then(response => {
        this.ring_health_planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planAssort: null,
        planName: null,
        planCycle: null,
        planProgress: null,
        planPoints: null,
        idDefault: null,
        planStatus: "0",
        stepCount: null,
        sleepQuality: null,
        restingHeartRate: null,
        heartRateVariability: null,
        bloodOxygenSaturation: null,
        calorie: null,
        checkInEvent: null,
        emotionManagement: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加健康计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRing_health_plan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改健康计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRing_health_plan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRing_health_plan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除健康计划编号为"' + ids + '"的数据项？').then(function() {
        return delRing_health_plan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ring/ring_health_plan/export', {
        ...this.queryParams
      }, `ring_health_plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
